[GIT101] Git 超新手入門


Posted by Torai on 2021-04-18

git init #在所在資料夾下初始化 git
git status #確認目前狀態

git add #把檔案加入版本控制
git add . #把目前資料夾底下的檔案全部加入 stage
#untrack 不加入
#stage 加入版本控制
git rm --cached file #把stage的檔案變成 untrack

git commit -m "commit message" #新建一個版本  
#-a 將所有 stage 區的檔案都 add,再 commit (就不用 git add .)
git commit --amend # 太早提交,接著才發現忘了加入某些檔案,或者寫錯了提交訊息,想要重新提交

git reset <version>  #回到某個版本的 commit,也可以使用 HEAD^,代表當前的上一個版本。
#--hard 完全回到上一個 commit 的檔案狀態
#--mixed 保留變更的檔案,移除 commit 和 add 的動作(預設)
#--soft 保留變更的檔案,僅移除 commit 的動作

git log #歷史紀錄 --oneline 簡短的顯示紀錄
git log --pretty=format:"%h %cd %cn %s" --graph #自訂格式
git log --oneline --decorate --graph --all

git checkout <version> #回到某個 commit
git checkout --<file> #還沒 commit,但改的東西我不要了

git diff #比較現在檔案與上次提交的不同之處

# get manual
git help <verb>
git <verb> --help
man git-<verb>

Branch

Git Book

git branch new-feature #開一條叫做 new-branch 的分支
git branch -v #查看分支 -vv 可查看 tracking branch 的狀態
git branch -m <newbrench_name> #修改 branch 的名稱
git branch -d new-feature #刪除 new-feature分支
git branch -u <upstream> [<branchname>] #重新設定 branch 的 upstream branch

git checkout <branch> #切換到某個分支
git checkout -b <newbranch_name> #新創一個分支並切換

#serverfix 是 tracking branch, origin/serverfix 是 upstream branch
#才能在 tracking branch 上編輯, upstream branch 不能進行編輯
git checkout -b serverfix origin/serverfix 

git merge new-feature #把 new-feature 分支合併進來(到目前分支內)

Remote

#push an existing repository from the  command line

#新增一個遠端的 repository,位置是 url,代號是 origin
git remote add origin <github_repository_url.git>

#把檔案推上遠端,位於 origin 的 feature 分支
git push -u origin feature
# push your local serverfix branch to the awesomebranch branch on the remote project.
git push origin serverfix:awesomebranch 

#將遠端的內容拉下來
git pull origin master

#將遠端的專案複製一份下來
#此時用 git push origin master 會推回 github 原本的 repository,可能因為沒有權限被拒絕
#可以 fork 一份到自己的帳號底下,再 clone 一份,這樣 push 的時候就是推到自己的 repository
git clone url

pull request: 合併分支,大概等於 git merge new-feature 的功能,在 GitHub的頁面中操作,也可推回原本別人的專案內。

GitHub Page

可以把 repository 變成靜態網頁,就不用另外租伺服器架網頁。
在 repository 的 setting 底下。

GitHub Flow

  1. Create a brench
  2. Add commits
  3. Open the pull request
  4. Discuss and review your code
  5. Deploy
  6. Merge

Git hook

在事件發生時通知我 or 做出反應。
在 .git/hooks底下,可以寫 shell script 對特定情況作出反應。


#Git







Related Posts

Simple Twitter 專案開發心得

Simple Twitter 專案開發心得

初識 shell script

初識 shell script

LeetCode JS Easy 2704. To Be Or Not To Be

LeetCode JS Easy 2704. To Be Or Not To Be


Comments